package korkd.tower;

import java.util.LinkedList;

import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.util.path.Path;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.pool.GenericPool;

public class UnitPool extends GenericPool<Unit> {

	private TiledTextureRegion mTextureRegion;
	private Scene mScene;
	private Path mPath;
	private LinkedList<Unit> mUnitList;
	private int mCount;
	private int mCastleHealth;
	private int[] mTileDimensions = new int[2];
	
	UnitPool(TiledTextureRegion pTextureRegion, Scene pScene, TMXTiledMap pTMXTiledMap, Path pPath, int pCastleHealth)
	{
		mTextureRegion = pTextureRegion;
		mScene = pScene;
		mPath = pPath;
		setCastleHealth(pCastleHealth);
		mUnitList = new LinkedList<Unit>();
		mTileDimensions[0] = pTMXTiledMap.getTileWidth();
		mTileDimensions[1] = pTMXTiledMap.getTileHeight();
		mCount = 0;

		
	}
	
	@Override
	protected Unit onAllocatePoolItem() {
		final Unit tUnit = new Unit(0, 0, mTextureRegion.deepCopy(), this);
		mScene.attachChild(tUnit);
		return tUnit;
	}
	
	@Override
	protected void onHandleRecycleItem(final Unit pUnit)
	{
		pUnit.setVisible(false);
		pUnit.setDead(true);
		pUnit.setIgnoreUpdate(true);
		mCount++;
		mUnitList.remove(pUnit);
	}
	
	@Override
	protected void onHandleObtainItem(final Unit pUnit)
	{
		pUnit.setVisible(true);
		pUnit.setIgnoreUpdate(false);
		pUnit.setHealth(300);
		pUnit.setDead(false);
		pUnit.getHealthBar().setWidth(pUnit.getWidth());
		mUnitList.add(pUnit);
		
		pUnit.registerEntityModifier(new AStarPathModifier(40, mPath, mTileDimensions, new AStarPathModifier.IAStarPathModifierListener()
		{
			@Override
	        public void onPathWaypointStarted(AStarPathModifier pPathModifier, final IEntity pEntity, int pWaypointIndex) {
        	}

			@Override
			public void onPathStarted(AStarPathModifier pPathModifier,
					IEntity pEntity) {
				
			}

			@Override
			public void onNextMoveLeft(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				pUnit.animate(new long[]{200, 200, 200}, 9, 11, true);
				
			}

			@Override
			public void onNextMoveUp(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				pUnit.animate(new long[]{200, 200, 200}, 0, 2, true);
				
			}

			@Override
			public void onNextMoveRight(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				pUnit.animate(new long[]{200, 200, 200}, 3, 5, true);
				
			}

			@Override
			public void onNextMoveDown(AStarPathModifier aStarPathModifier,
					IEntity pEntity, int pIndex) {
				pUnit.animate(new long[]{200, 200, 200}, 6, 8, true);
				
			}

			@Override
			public void onPathWaypointFinished(AStarPathModifier pPathModifier,
					IEntity pEntity, int pWaypointIndex) {
			}

			@Override
			public void onPathFinished(AStarPathModifier pPathModifier,
					IEntity pEntity) {
				mCastleHealth--;
				pUnit.attack();
			}
		}));
	}
	
	public boolean isInUnitList(Unit u)
	{
		boolean result = false;
		
		if(mUnitList.contains(u))
		{
			result = true;
		}
		
		return result;
	}
	
	public LinkedList<Unit> getUnitList()
	{
		return mUnitList;
	}
	
	public int getCount()
	{
		return mCount;
	}

	public int getCastleHealth() {
		return mCastleHealth;
	}

	public void setCastleHealth(int mCastleHealth) {
		this.mCastleHealth = mCastleHealth;
	}

}
